const app = getApp();

Page({
    data: {
        productList: []
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '商品管理'
        })
        this.getProductList()
    },
    onShow:function (){
        this.getProductList()
    },
    getProductList (){
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/product/getProductList',
            data: {
                token: app.globalData.token,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                //停止刷新
                // wx.stopPullDownRefresh();
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        productList:res.data.productList
                    })
                }
            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },
    addProduct (){
        wx.navigateTo({
            url:'/pages/product-add/product-add'
        })
    },
    deleteProduct (e){
        var that = this;
        wx.showModal({
            title: '确定删除商品"'+ e.currentTarget.dataset.name +'"吗？',
            content: '商品删除后不可恢复，确定删除商品吗',
            confirmText:'删除',
            confirmColor:'#F76260',
            showCancel: true,
            success: function (res) {
                if (res.confirm) {
                    that.gotoDeleteProduct(e.currentTarget.dataset.id);
                }
            }
        });
    },
    gotoDeleteProduct(id){
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/product/delProduct',
            data: {
                token: app.globalData.token,
                id:id
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                //停止刷新
                // wx.stopPullDownRefresh();
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showModal({
                        title: '删除成功',
                        showCancel: false,
                        success: function (res) {
                        }
                    });
                    that.getProductList();
                }
            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    }

})